using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Cam;
using net_work.IService.Cam;

namespace net_work.Controllers.Cam;

[Route("api/[controller]")]
[ApiController]
public class ApplyController(ILogger<ApplyController> logger, IApplyService applyService) : ControllerBase
{
    /// <summary>
    /// 获取审批申请列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListApplys([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取审批申请列表");
        var result = await applyService.GetApplysPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取审批申请信息
    /// </summary>
    /// <param name="applyId">审批申请ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetApplyById([FromQuery]long applyId)
    {
        logger.LogInformation("获取审批申请信息");
        return await applyService.SelectByIdAsync(applyId) is { } apply
            ? AjaxResult.Success(apply)
            : AjaxResult.Error();
    }
    
    /// <summary>
    /// 新增审批申请
    /// </summary>
    /// <param name="applyAddRequest">新增审批申请实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> AddApply([FromBody] ApplyAddRequest applyAddRequest)
    {
        logger.LogInformation("新增审批申请");
        var applyId = await applyService.InsertApply(applyAddRequest);
        return applyId > 0
            ? AjaxResult.Success(new { applyId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改审批申请
    /// </summary>
    /// <param name="applyUpdateRequest">修改审批申请实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> UpdateApply([FromBody] ApplyUpdateRequest applyUpdateRequest)
    {
        logger.LogInformation("修改审批申请");
        return await applyService.UpdateApply(applyUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除审批申请
    /// </summary>
    /// <param name="ids">审批申请ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> DeleteApply([FromBody] List<long> ids)
    {
        logger.LogInformation("删除审批申请");
        return await applyService.DeleteApplyByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}